package atguigu;

import java.util.HashMap;
import java.util.Map;

public class LeetCode001 {
    public static void main(String[] args) {
        int a[] = {2,7,11,13,15};
        int target = 9;
        int b[] = addSum1(a,target);
        System.out.println("[" + b[0] + "," + b[1] + "]");

        int c[] = addSum2(a,target);
        System.out.println("[" + c[0] + "," + c[1] + "]");
    }

    public static int[] addSum1(int[] sums,int target){
        for (int i = 0; i < sums.length; i++) {
            for (int j = i+1 ; j < sums.length; j++) {
                if (sums[i] + sums[j] == target){
                    return new int[]{i,j};
                }
            }
        }
        return null;
    }

    public static int[] addSum2(int[] sums,int target){
        Map<Integer, Integer> map = new HashMap<>(sums.length);
        for (int i = 0; i < sums.length; i++) {
            int sum = target - sums[i];
            if (map.containsKey(sum)){
                return new int[]{map.get(sum),i};
            }
            map.put(sums[i],i);
        }
        return null;
    }

}
